package com.jhgsys.internal.sypq.service.impl;

import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.common.utils.DateUtil;
import com.jhgsys.internal.sypq.entity.ResourceDto;
import com.jhgsys.internal.sypq.mapper.HomeMapper;
import com.jhgsys.internal.sypq.service.IHomeService;
import org.jsoup.helper.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 日历管理 Service实现
 *
 * @author ganjun
 * @date 2024-11-21 16:56:51
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HomeServiceImpl extends ServiceImpl<HomeMapper, Map> implements IHomeService {

    public Map<String, String> getScheduleStatusNum() {
      /*待审批任务：排期待审核
      已审批任务：已排期、实验中、已完成、已暂停、驳回
      已排期任务：已排期、实验中、已完成、已暂停*/
        Map<String,String>  p1=this.baseMapper.getP1Num();
        Map<String,String>  p3=this.baseMapper.getP3Num();
        Map<String,String>  p4=this.baseMapper.getP4Num();
        Map<String,String>  p5=this.baseMapper.getP5Num();
        Map  scheduleStatusMap=this.baseMapper.getScheduleStatusNum();
        scheduleStatusMap.put("p1",p1.get("pro"));
        scheduleStatusMap.put("p3",p3.get("pro"));
        scheduleStatusMap.put("p4",p4.get("pro"));
        scheduleStatusMap.put("p5",p5.get("pro"));
        return   scheduleStatusMap;
    }

    @Override
    public Map getResourceData(String resourceid, String weidu) {
        CompletableFuture<Map> future=CompletableFuture.supplyAsync(()->{
            Map map = new HashMap();
            Calendar calendar = Calendar.getInstance();
            if ("week".equals(weidu)) {
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 设置要获取的日期是周一
                LocalDate currentDate = LocalDate.now();
                WeekFields weekFields = WeekFields.of(Locale.getDefault());
                int currentWeek = currentDate.get(weekFields.weekOfWeekBasedYear());
                String[] weeks = new String[26];
                Double[] workday = new Double[26];
                Double[] unworkday = new Double[26];
                for (int i = 0; i < 26; i++) {
                //    calendar.set(Calendar.WEEK_OF_YEAR, i + currentWeek);
                    String  beginDate = DateUtil.getDateFormat(calendar.getTime(), "yyyy-MM-dd");
                    calendar.add(Calendar.DATE, 6);
                    String  endDate = DateUtil.getDateFormat(calendar.getTime(), "yyyy-MM-dd");
                    Map<String, Double> workdayMap = this.baseMapper.getResourceData(resourceid,beginDate, endDate);
                    String  yearStr= DateUtil.getDateFormat(calendar.getTime(), "yyyy");
                    if(currentWeek+i<53){
                        weeks[i] = yearStr + "_" + (currentWeek + i);
                    }else {
                        weeks[i] = yearStr + "_" + (currentWeek + i - 52);
                    }
                    if(workdayMap==null){
                        workday[i] = 0d;
                        unworkday[i] = 7d;
                    }else {
                        Double workNum= workdayMap.get("workday");
                        workday[i] =workNum;
                        unworkday[i] = 7 - workNum;
                    }
                }
                map.put("weeks", weeks);
                map.put("workday", workday);
                map.put("unworkday", unworkday);
                return map;
            } else {
                String[] weeks = new String[6];
                Double[] workday = new Double[6];
                Double[] unworkday = new Double[6];
                for (int i = 0; i < 6; i++) {
                    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
                    String  startdate = DateUtils.format(calendar.getTime(),"yyyy-MM-dd");
                    String  yearMonth=DateUtils.format(calendar.getTime(),"yyyy-MM");
                    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                    String enddate =DateUtils.format(calendar.getTime(),"yyyy-MM-dd");
                    Double day= Double.valueOf(enddate.substring(8,10));
                    Map<String, Double> workdayMap = this.baseMapper.getResourceData(resourceid,startdate, enddate);
                    if(workdayMap==null){
                        workday[i] = 0d;
                        unworkday[i] =day ;
                    }else {
                        workday[i] = workdayMap.get("workday");
                        unworkday[i] = day - workday[i];
                    }
                        weeks[i] = yearMonth;
                    calendar.add(Calendar.MONTH, 1);
                }
                map.put("weeks", weeks);
                map.put("workday", workday);
                map.put("unworkday", unworkday);
                return map;
            }

        });
        try {
            return  future.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public Map  getShipData(){
      CompletableFuture<Map> future=CompletableFuture.supplyAsync(()-> {
            String[] yearMonth = new String[12];
            Long[] num = new Long[12];
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-11);
            for (int i = 0; i < 12; i++) {
                Date date = calendar.getTime();
                yearMonth[i] = new SimpleDateFormat("yyyy-MM").format(date);
                Map<String, Long> shipMap = this.baseMapper.getShipData(yearMonth[i]);
                if (shipMap == null) {
                    num[i] = 0L;
                } else {
                    num[i] = shipMap.get("num");
                }
                calendar.add(Calendar.MONTH, 1);
            }
            Map map = new HashMap();
            map.put("yearMonth", yearMonth);
            map.put("num", num);
            return map;
    });
        try {
            return  future.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public  List<ResourceDto>  getResourceList(){
      List<ResourceDto>  list=    this.baseMapper.getResourceList();
       list.forEach(item->{
           if(item.getRsStatus()==2){
               //不处理
           }else if(StringUtil.isBlank(item.getTaskCode())){
               item.setRsStatus(3);
               item.setRsStatusName("空闲");
           }else {
               item.setRsStatus(1);
               item.setRsStatusName("占用");
           }
       });
         return  list;
    }
}
